package com.nyist.zk.controller;

import com.nyist.zk.entities.*;
import com.nyist.zk.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private BloodManagerService bloodManagerService;
    @Autowired
    private PatientService patientService;
    @Autowired
    private BloodService bloodService;
    @Autowired
    private UseBloodService useBloodService;

    @PostMapping("/admins/login")
    public String AdminLogin(HttpSession session, Admin admin, Map<String, Object> map) {
        if (!StringUtils.isEmpty(admin.getA_id())
                && !StringUtils.isEmpty(admin.getA_password())) {
            boolean flag = adminService.getAdminByIdAndPassword(admin.getA_id(),admin.getA_password());
            if (flag) {
                //登录成功
                Admin byIdAdminMap = adminService.findByIdAdmin(admin.getA_id());
                session.setAttribute("adminLoginUser",byIdAdminMap);
                System.out.println("登录成功!");
                return "admin/adminIndex";
            }
        }
        //登录失败
        map.put("msg", "用户名或密码错误");
        return "admin/adminLogin";
    }

    @GetMapping("/admin/{a_id}")
    public String findByIdAdmin(@PathVariable("a_id") Integer a_id,Map<String, Object> map) {
        System.out.println("--------查询id为  :  " + a_id + " 超级管理员---------");
        Admin byIdAdminMap = adminService.findByIdAdmin(a_id);
        map.put("byIdAdminMap", byIdAdminMap);
        return "admin/adminList";
    }

    @GetMapping("/adminUpdates/{a_id}")
    public String toAdminUpdate(@PathVariable("a_id") Integer a_id, Map<String, Object> map) {
        System.out.println("--------准备修改医生个人信息---------");
        Admin byIdAdminMap = adminService.findByIdAdmin(a_id);
        map.put("byIdAdminMap", byIdAdminMap);
        return "admin/adminUpdate";
    }

    @PutMapping("/adminUpdate")
    public String updateAdmin(Admin admin, Map<String, Object> map) {
        System.out.println("--------------修改管理员个人基本信息----------------");
        adminService.updateAdmin(admin);
        Admin byIdAdminMap = adminService.findByIdAdmin(admin.getA_id());
        map.put("byIdAdminMap", byIdAdminMap);
        return "admin/adminList";
    }

    @GetMapping("/admin/pwd")
    public String updateAdminPassword() {
        System.out.println("--------------准备修改密码-----------------");
        return "admin/adminPassword";
    }

    @ResponseBody
    @GetMapping("/admin/pwd/{oldPwd}")
    public Boolean checkAdminPwd(HttpSession session, @PathVariable("oldPwd") String oldPwd) {
        System.out.println("原来的密码:"+oldPwd);
        //1.从Session中获取当前登录用户的User对象
        Admin admin = (Admin) session.getAttribute("adminLoginUser");
        if(admin.getA_password().equals(oldPwd)) {
            //输入的旧密码正确
            return true;
        }
        return false;
    }

    @PostMapping("/admin/pwd")
    public String updateAdminPwd(HttpSession session, String password) {
        //1.从Session中获取当前登录用户信息
        Admin admin = (Admin) session.getAttribute("adminLoginUser");
        //2. 更新密码
        admin.setA_password(password);
        System.out.println("新的密码是:"+password);
        //3. 提交到数据库
        adminService.updateAdmin(admin);
        //4. 注销重新登录
        return "redirect:/admin";
    }
    /***************************超级管理员对医生的操作开始******************************/

    @GetMapping("/adminFindDoctor")
    public String adminFindAllDoctor(Map<String, Object> map) {
        System.out.println("--------超级管理员查看个人医生信息---------");
        List<Doctor> byIdDoctorMap = doctorService.findAllDoctor();
        map.put("byIdDoctorMap", byIdDoctorMap);
        return "admin/adminFindDoctorList";
    }

    @GetMapping("/adminFindDoctorByNameLike")
    public String adminFindDoctorByNameLike(Map<String, Object> map,Doctor doctor) {
        System.out.println("--------超级管理员查看个人医生信息模糊查找---------");
        List<Doctor> byIdDoctorMap = doctorService.findDoctorByNameLike(doctor.getD_name());
        map.put("byIdDoctorMap", byIdDoctorMap);
        return "admin/adminFindDoctorList";
    }

    @GetMapping("/adminDoctorUpdates/{d_id}")
    public String toUpdate(@PathVariable("d_id") Integer d_id, Map<String, Object> map) {
        System.out.println("--------超级管理员准备修改医生个人信息---------");
        Doctor byIdDoctorMap = doctorService.findByIdDoctor(d_id);
        map.put("byIdDoctorMap", byIdDoctorMap);
        return "admin/adminDoctorUpdate";
    }

    @PutMapping("/adminDoctorUpdate")
    public String adminDoctorUpdate(Doctor doctor, Map<String, Object> map) {
        System.out.println("--------------超级管理员修改医生个人基本信息----------------");
        doctorService.updateDoctor(doctor);
        List<Doctor> byIdDoctorMap = doctorService.findAllDoctor();
        map.put("byIdDoctorMap", byIdDoctorMap);
        return "admin/adminFindDoctorList";
    }

    //前往添加 页面
    @GetMapping("/toAdminAddDoctorPage")
    public String toAdminAddDoctorPage() {
        System.out.println("----------------超级管理员准备添加医生信息-------------------");
        return "admin/adminAddDoctor";
    }

    //添加数据
    @PostMapping("/adminAddDoctorData")
    public String adminAddDoctorData(Doctor doctor, Map<String, Object> map) {
        //保存数据操作
        System.out.println("---------------超级管理员添加医生信息----------------------------");
        doctorService.insertDoctor(doctor);
        List<Doctor> byIdDoctorMap = doctorService.findAllDoctor();
        map.put("byIdDoctorMap", byIdDoctorMap);
        return "admin/adminFindDoctorList";
    }

    @DeleteMapping("/adminDeleteDoctorData/{d_id}")
    public String adminDeleteDoctorData(@PathVariable("d_id") Integer d_id, Map<String, Object> map) {
        doctorService.deleteByIdDoctor(d_id);
        List<Doctor> byIdDoctorMap = doctorService.findAllDoctor();
        map.put("byIdDoctorMap", byIdDoctorMap);
        return "admin/adminFindDoctorList";
    }
    /**********************超级管理员操作医生结束****************************************/

    /**********************超级管理员操作血库管理员开始****************************************/
    @GetMapping("/adminFindBloodManager")
    public String adminFindAllBloodManager(Map<String, Object> map) {
        System.out.println("--------超级管理员查看血库管理员信息---------");
        List<BloodManager> byIdBloodManagerMap = bloodManagerService.findAllBloodManager();
        map.put("byIdBloodManagerMap", byIdBloodManagerMap);
        return "admin/adminFindBloodManagerList";
    }

    @GetMapping("/adminFindBloodManagerByNameLike")
    public String adminFindBloodManagerByNameLike(Map<String, Object> map,BloodManager bloodManager) {
        System.out.println("--------超级管理员查看血库管理员信息模糊查找---------");
        List<BloodManager> byIdBloodManagerMap = bloodManagerService.findBloodManagerByNameLike(bloodManager.getBm_name());
        for (BloodManager BloodManager : byIdBloodManagerMap) {
            System.out.println(BloodManager);
        }
        map.put("byIdBloodManagerMap", byIdBloodManagerMap);
        return "admin/adminFindBloodManagerList";
    }

    @GetMapping("/adminBloodManagerUpdates/{bm_id}")
    public String adminBloodManagerUpdates(@PathVariable("bm_id") Integer bm_id, Map<String, Object> map) {
        System.out.println("--------超级管理员准备修改血库管理员个人信息---------");
        BloodManager byIdBloodManagerMap = bloodManagerService.findByIdBloodManager(bm_id);
        map.put("byIdBloodManagerMap", byIdBloodManagerMap);
        return "admin/adminBloodManagerUpdate";
    }

    @PutMapping("/adminBloodManagerUpdate")
    public String adminBloodManagerUpdate(BloodManager bloodManager, Map<String, Object> map) {
        System.out.println("--------------超级管理员修改血库管理员个人基本信息----------------");
        bloodManagerService.updateBloodManager(bloodManager);
        List<BloodManager> byIdBloodManagerMap = bloodManagerService.findAllBloodManager();
        map.put("byIdBloodManagerMap", byIdBloodManagerMap);
        return "admin/adminFindBloodManagerList";
    }

    //前往添加 页面
    @GetMapping("/toAdminAddBloodManagerPage")
    public String toAdminAddBloodManagerPage() {
        System.out.println("----------------超级管理员准备添加血库管理员信息-------------------");
        return "admin/adminAddBloodManager";
    }

    //添加数据
    @PostMapping("/adminAddBloodManagerData")
    public String adminAddBloodManagerData(BloodManager bloodManager, Map<String, Object> map) {
        //保存数据操作
        System.out.println("---------------超级管理员添加血库管理员信息----------------------------");
        bloodManagerService.insertBloodManager(bloodManager);
        List<BloodManager> byIdBloodManagerMap = bloodManagerService.findAllBloodManager();
        map.put("byIdBloodManagerMap", byIdBloodManagerMap);
        return "admin/adminFindBloodManagerList";
    }

    @DeleteMapping("/adminDeleteBloodManagerData/{bm_id}")
    public String adminDeleteBloodManagerData(@PathVariable("bm_id") Integer bm_id, Map<String, Object> map) {
        bloodManagerService.deleteByIdBloodManager(bm_id);
        List<BloodManager> byIdBloodManagerMap = bloodManagerService.findAllBloodManager();
        map.put("byIdBloodManagerMap", byIdBloodManagerMap);
        return "admin/adminFindBloodManagerList";
    }
    /**********************超级管理员操作血库管理员结束****************************************/

    /**********************超级管理员操作病人开始****************************************/

    @GetMapping("/adminFindPatient")
    public String adminFindPatient(Map<String, Object> map) {
        System.out.println("--------超级管理员查看病人信息---------");
        List<Patient> byIdPatientMap = patientService.findAllPatient();
        map.put("byIdPatientMap", byIdPatientMap);
        return "admin/adminFindPatientList";
    }

    @DeleteMapping("/adminDeletePatientData/{p_id}")
    public String adminDeletePatientData(@PathVariable("p_id") Integer p_id, Map<String, Object> map) {
        patientService.deleteByIdPatient(p_id);
        List<Patient> byIdPatientMap = patientService.findAllPatient();
        map.put("byIdPatientMap", byIdPatientMap);
        return "admin/adminFindPatientList";
    }

    @GetMapping("/adminFindPatientByNameLike")
    public String adminFindPatientByNameLike(Map<String, Object> map,Patient patient) {
        System.out.println("--------超级管理员查看病人信息模糊查找---------");
        List<Patient> byIdPatientMap = patientService.adminFindPatientByNameLike(patient.getP_name());
        map.put("byIdPatientMap", byIdPatientMap);
        return "admin/adminFindPatientList";
    }

    @GetMapping("/adminPatientUpdates/{p_id}")
    public String adminPatientUpdates(@PathVariable("p_id") Integer p_id, Map<String, Object> map) {
        System.out.println("--------超级管理员准备修改病人个人信息---------");
        Patient byIdPatientMap = patientService.findByIdPatient(p_id);
        map.put("byIdPatientMap", byIdPatientMap);
        return "admin/adminPatientUpdate";
    }

    @PutMapping("/adminPatientUpdate")
    public String adminPatientUpdate(Patient patient, Map<String, Object> map) {
        System.out.println("--------------超级管理员修改病人基本信息----------------");
        patientService.updatePatient(patient);
        List<Patient> byIdPatientMap = patientService.findAllPatient();
        map.put("byIdPatientMap", byIdPatientMap);
        return "admin/adminFindPatientList";
    }

    /**********************超级管理员操作病人结束****************************************/

    /**********************超级管理员操作血库开始****************************************/

    @GetMapping("/adminFindBlood")
    public String adminFindBlood(Map<String, Object> map) {
        System.out.println("--------超级管理员查看血库信息---------");
        List<Blood> byIdBloodMap = bloodService.findAllBlood();
        map.put("byIdBloodMap", byIdBloodMap);
        return "admin/adminFindBloodList";
    }

    /**********************超级管理员操作血库结束****************************************/

    /**********************超级管理员操作血库申请开始****************************************/

    @GetMapping("/adminFindUseBlood")
    public String adminFindUseBlood(Map<String, Object> map) {
        System.out.println("--------超级管理员查看血库申请信息---------");
        List<UseBlood> byIdUseBloodMap = useBloodService.findAllUseBlood();
        map.put("byIdUseBloodMap", byIdUseBloodMap);
        return "admin/adminFindUseBloodList";
    }

}
